// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 15:14:18
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Material\MAT9Parser.cpp
 */
#include "Import/BDFImport/BulkData/Material/MAT9Parser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Material/IAnisotropic3DMaterialService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (MAT9);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/MAT9/MAT9.md>
*/
enum MAT9key : std::uint8_t
{
    MAT9Key_MID  = 2,   // 材料识别号
    MAT9Key_G11  = 3,   // G11 元素
    MAT9Key_G12  = 4,   // G12 元素
    MAT9Key_G13  = 5,   // G13 元素
    MAT9Key_G14  = 6,   // G14 元素
    MAT9Key_G15  = 7,   // G15 元素
    MAT9Key_G16  = 8,   // G16 元素
    MAT9Key_G22  = 9,   // G22 元素
    MAT9Key_G23  = 12,  // G23 元素
    MAT9Key_G24  = 13,  // G24 元素
    MAT9Key_G25  = 14,  // G25 元素
    MAT9Key_G26  = 15,  // G26 元素
    MAT9Key_G33  = 16,  // G33 元素
    MAT9Key_G34  = 17,  // G34 元素
    MAT9Key_G35  = 18,  // G35 元素
    MAT9Key_G36  = 19,  // G36 元素
    MAT9Key_G44  = 22,  // G44 元素
    MAT9Key_G45  = 23,  // G45 元素
    MAT9Key_G46  = 24,  // G46 元素
    MAT9Key_G55  = 25,  // G55 元素
    MAT9Key_G56  = 26,  // G56 元素
    MAT9Key_G66  = 27,  // G66 元素
    MAT9Key_RHO  = 28,  // 质量密度
    MAT9Key_A1   = 29,  // A1 热膨胀系数
    MAT9Key_A2   = 32,  // A2 热膨胀系数
    MAT9Key_A3   = 33,  // A3 热膨胀系数
    MAT9Key_A4   = 34,  // A4 热膨胀系数
    MAT9Key_A5   = 35,  // A5 热膨胀系数
    MAT9Key_A6   = 36,  // A6 热膨胀系数
    MAT9Key_TREF = 37,  // 参考温度
    MAT9Key_GE   = 38,  // 结构阻尼系数
    MAT9Key_GE11 = 42,  // GE11 阻尼矩阵元素
    MAT9Key_GE12 = 43,  // GE12 阻尼矩阵元素
    MAT9Key_GE13 = 44,  // GE13 阻尼矩阵元素
    MAT9Key_GE14 = 45,  // GE14 阻尼矩阵元素
    MAT9Key_GE15 = 46,  // GE15 阻尼矩阵元素
    MAT9Key_GE16 = 47,  // GE16 阻尼矩阵元素
    MAT9Key_GE22 = 48,  // GE22 阻尼矩阵元素
    MAT9Key_GE23 = 49,  // GE23 阻尼矩阵元素
    MAT9Key_GE24 = 52,  // GE24 阻尼矩阵元素
    MAT9Key_GE25 = 53,  // GE25 阻尼矩阵元素
    MAT9Key_GE26 = 54,  // GE26 阻尼矩阵元素
    MAT9Key_GE33 = 55,  // GE33 阻尼矩阵元素
    MAT9Key_GE34 = 56,  // GE34 阻尼矩阵元素
    MAT9Key_GE35 = 57,  // GE35 阻尼矩阵元素
    MAT9Key_GE36 = 58,  // GE36 阻尼矩阵元素
    MAT9Key_GE44 = 59,  // GE44 阻尼矩阵元素
    MAT9Key_GE45 = 62,  // GE45 阻尼矩阵元素
    MAT9Key_GE46 = 63,  // GE46 阻尼矩阵元素
    MAT9Key_GE55 = 64,  // GE55 阻尼矩阵元素
    MAT9Key_GE56 = 65,  // GE56 阻尼矩阵元素
    MAT9Key_GE66 = 66,  // GE66 阻尼矩阵元素
};

Common::Status BDF::BULKDATA::ParseMAT9 (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                         std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建一个用于保存MAT9材料数据的临时结构体对象
    FEM::Anisotropic3DMaterial tempData;

    // TODO(bug 001) 填写m_domainId的正确值
    // 目前m_domainId硬编码为1，需要进一步确认是否正确
    tempData.m_domainId = 1;

    int curIDKey = 1;  // 当前处理的条目索引

    // 遍历所有分割后的条目信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 检查当前条目是否为空
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

                // 根据条目ID，将解析后的数值存入对应的MAT9字段
        switch (curIDKey)
        {
        case MAT9Key_MID:
            tempData.m_id = convertToI64 (curStrKey);
            break;
        case MAT9Key_G11:
            tempData.m_G[0] = convertToReal (curStrKey);
            break;
        case MAT9Key_G12:
            tempData.m_G[1] = convertToReal (curStrKey);
            break;
        case MAT9Key_G13:
            tempData.m_G[2] = convertToReal (curStrKey);
            break;
        case MAT9Key_G14:
            tempData.m_G[3] = convertToReal (curStrKey);
            break;
        case MAT9Key_G15:
            tempData.m_G[4] = convertToReal (curStrKey);
            break;
        case MAT9Key_G16:
            tempData.m_G[5] = convertToReal (curStrKey);
            break;
        case MAT9Key_G22:
            tempData.m_G[6] = convertToReal (curStrKey);
            break;
        case MAT9Key_G23:
            tempData.m_G[7] = convertToReal (curStrKey);
            break;
        case MAT9Key_G24:
            tempData.m_G[8] = convertToReal (curStrKey);
            break;
        case MAT9Key_G25:
            tempData.m_G[9] = convertToReal (curStrKey);
            break;
        case MAT9Key_G26:
            tempData.m_G[10] = convertToReal (curStrKey);
            break;
        case MAT9Key_G33:
            tempData.m_G[11] = convertToReal (curStrKey);
            break;
        case MAT9Key_G34:
            tempData.m_G[12] = convertToReal (curStrKey);
            break;
        case MAT9Key_G35:
            tempData.m_G[13] = convertToReal (curStrKey);
            break;
        case MAT9Key_G36:
            tempData.m_G[14] = convertToReal (curStrKey);
            break;
        case MAT9Key_G44:
            tempData.m_G[15] = convertToReal (curStrKey);
            break;
        case MAT9Key_G45:
            tempData.m_G[16] = convertToReal (curStrKey);
            break;
        case MAT9Key_G46:
            tempData.m_G[17] = convertToReal (curStrKey);
            break;
        case MAT9Key_G55:
            tempData.m_G[18] = convertToReal (curStrKey);
            break;
        case MAT9Key_G56:
            tempData.m_G[19] = convertToReal (curStrKey);
            break;
        case MAT9Key_G66:
            tempData.m_G[20] = convertToReal (curStrKey);
            break;
        case MAT9Key_RHO:
            tempData.m_rho = convertToReal (curStrKey);
            break;
        case MAT9Key_A1:
            tempData.m_Ai[0] = convertToReal (curStrKey);
            break;
        case MAT9Key_A2:
            tempData.m_Ai[1] = convertToReal (curStrKey);
            break;
        case MAT9Key_A3:
            tempData.m_Ai[2] = convertToReal (curStrKey);
            break;
        case MAT9Key_A4:
            tempData.m_Ai[3] = convertToReal (curStrKey);
            break;
        case MAT9Key_A5:
            tempData.m_Ai[4] = convertToReal (curStrKey);
            break;
        case MAT9Key_A6:
            tempData.m_Ai[5] = convertToReal (curStrKey);
            break;
        case MAT9Key_TREF:
            tempData.m_tref = convertToReal (curStrKey);
            break;
        case MAT9Key_GE:
            tempData.m_ge = convertToReal (curStrKey);
            break;
        case MAT9Key_GE11:
            tempData.m_GE[0] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE12:
            tempData.m_GE[1] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE13:
            tempData.m_GE[2] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE14:
            tempData.m_GE[3] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE15:
            tempData.m_GE[4] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE16:
            tempData.m_GE[5] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE22:
            tempData.m_GE[6] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE23:
            tempData.m_GE[7] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE24:
            tempData.m_GE[8] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE25:
            tempData.m_GE[9] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE26:
            tempData.m_GE[10] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE33:
            tempData.m_GE[11] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE34:
            tempData.m_GE[12] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE35:
            tempData.m_GE[13] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE36:
            tempData.m_GE[14] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE44:
            tempData.m_GE[15] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE45:
            tempData.m_GE[16] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE46:
            tempData.m_GE[17] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE55:
            tempData.m_GE[18] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE56:
            tempData.m_GE[19] = convertToReal (curStrKey);
            break;
        case MAT9Key_GE66:
            tempData.m_GE[20] = convertToReal (curStrKey);
            break;
        default:
            break;
        }
        curIDKey++;
    }

    // 将解析后的MAT9数据通过数据库服务工厂的Anisotropic3DMaterial服务添加到数据库中
    return dbServiceFactory->get<IAnisotropic3DMaterialService> ()->Add (tempData);
}
